﻿//**************************************************************
//
// MoneyBaby Project - Open source payment processors for .NET
//
// Copyright 2007-2008 Marcus McConnell and BV Software
// www.CodePlex.com/MoneyBaby
//**************************************************************

// This is a simple command line program that demonstrates how to use MoneyBaby to process payments

using System;
using System.Collections.Generic;
using System.Text;
using BVSoftware.MoneyBaby;
using BVSoftware.MoneyBaby.Gateways;

namespace MoneyBabySample
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("");
            Console.WriteLine("//**************************************************************");
            Console.WriteLine("//");
            Console.WriteLine("// MoneyBaby Project - Open source payment processors for .NET");
            Console.WriteLine("//");
            Console.WriteLine("// Copyright 2007-2008 Marcus McConnell and BV Software");
            Console.WriteLine("// www.CodePlex.com/MoneyBaby");
            Console.WriteLine("//**************************************************************");
            Console.WriteLine("");

            AuthorizeNetSample();
        }

        private static void AuthorizeNetSample()
        {
            Console.WriteLine("Authorize.Net Sample Running");

            ICreditCardGateway<AuthorizeNetGatewaySettings> authNet 
                = CreditCardProcessor.GetGateway<AuthorizeNet,AuthorizeNetGatewaySettings>();            

            PaymentData<AuthorizeNetGatewaySettings> data = authNet.PaymentData;

            // Configure authorize.net options
            data.GatewaySettings.EmailCustomer = false;           
            data.GatewaySettings.Username = "MyLoginId";                //Authorize.Net Login Id
            data.GatewaySettings.Password = "MyTransactionKeyGoesHere"; //Authorize.Net Transaction Key
            data.GatewaySettings.TestMode = true;                       // True = testing, False = Live processing
       
            // Optionally set a url if it has changed from the compiled url
            //data.GatewaySettings.UrlLive = "http://";
            //data.GatewaySettings.UrlTest = "http://";

            // Set Credit Card Info
            data.CardData.CardHolderName = "John Q. Public";
            data.CardData.CardNumber = "41111111111111";
            data.CardData.ExpirationMonth = 1;
            data.CardData.ExpirationYear = 2009;
            data.CardData.SecurityCode = "1234";

            // Set customer billing data
            data.Customer.EmailAddress = "test@donotresolve.com";
            data.Customer.FirstName = "John";
            data.Customer.LastName = "Public";
            //data.Customer.Company = "Acme Corp.";
            data.Customer.AddressLine1 = "123 Fake Street";
            //data.Customer.AddressLine2 = "Suite 1234";
            data.Customer.City = "MyCity";
            data.Customer.State = "ZZ";
            data.Customer.PostalCode = "12345";
            data.Customer.Country = "US";                       // Use ISO Code
            data.Customer.CustomerId = "test@donotresolve.com"; // Choose something unique to this customer
            data.Customer.IPAddress = "127.0.0.1";              // for tracking
            //data.Customer.PhoneNumber = "1234567890";
            //data.Customer.FaxNumber = "1234567890";


            // Tranasction Settings
            data.Transaction.Amount = 1.23M;                                // The actual amount to charge, refund, auth, capture or void
            data.Transaction.MerchantDescription = "Order From MoneyBaby";  // a description for the transaction
            data.Transaction.MerchantInvoiceNumber = "ABC123";              // a unique transaction id from you
            //data.Transaction.PreviousTransactionReferenceNumber = "123";  // Required for VOID and REFUND transactions. 

            
            // Call your desired action
            TransactionResultData result = authNet.Charge();
            if (result.Suceeded)
            {
                Console.WriteLine("Success!");
            }
            else
            {
                Console.WriteLine("Failed!");
                Console.WriteLine(result.ResponseCodeDescription);
                foreach (BVSoftware.MoneyBaby.TransactionMessage m in result.Messages)
                {
                    switch (m.MessageType)
                    {
                        case BVSoftware.MoneyBaby.TransactionMessageType.Error:
                            Console.WriteLine("ERROR:");
                            break;
                        case BVSoftware.MoneyBaby.TransactionMessageType.Warning:
                            Console.WriteLine("WARNING:");
                            break;
                        case BVSoftware.MoneyBaby.TransactionMessageType.Information:
                            Console.WriteLine("INFO:");
                            break;
                        default:
                            Console.WriteLine("Other:");
                            break;
                    }
                    Console.WriteLine(m.Description);
                }
            }
           
            Console.WriteLine("**********************************");
            Console.WriteLine(result.AVSCode);                  // Address Verification Code
            Console.WriteLine(result.AVSCodeDescription);       // Friendly description of AVS
            Console.WriteLine(result.CVVCode);                  // Security Code Results Code
            Console.WriteLine(result.CVVCodeDescription);       // Friendly description of CVV Code
            Console.WriteLine(result.ResponseCode);             // Response Code from Gateway for this transaction only
            Console.WriteLine(result.ResponseCodeDescription);  // Friendly description of response code
            // You'll want to store the transaction reference number from all calls so 
            // it can be used later in refunds and voids
            Console.WriteLine(result.TransactionReferenceNumber);


            Console.WriteLine("");
            Console.WriteLine("Finished Authorize.Net Sample");
        }

        private static void AuthorizeNetDynamicSample()
        {
            Console.WriteLine("Authorize.Net Sample Running");            

            ICreditCardGateway authNet = CreditCardProcessor.GetGateway(CreditCardProcessorType.AuthorizeNet);
            authNet.PaymentData.GatewaySettings.AddOrUpdateValue("Username", "justin");
            authNet.PaymentData.GatewaySettings.AddOrUpdateValue("Password", "mypass");
            authNet.PaymentData.GatewaySettings.AddOrUpdateValue("EmailCustomer", true);            

            // Configure authorize.net options
            authNet.PaymentData.GatewaySettings.AddOrUpdateValue("EmailCustomer", false);
            authNet.PaymentData.GatewaySettings.AddOrUpdateValue("Username", "MyLoginId"); //Authorize.Net Login Id
            authNet.PaymentData.GatewaySettings.AddOrUpdateValue("Password", "MyTransactionKeyGoesHere"); //Authorize.Net Transaction Key            
            authNet.PaymentData.GatewaySettings.TestMode = true;                       // True = testing, False = Live processing

            // Optionally set a url if it has changed from the compiled url
            //authNet.PaymentData.GatewaySettings.UrlLive = "http://";
            //authNet.PaymentData.GatewaySettings.UrlTest = "http://";

            // Set Credit Card Info
            authNet.PaymentData.CardData.CardHolderName = "John Q. Public";
            authNet.PaymentData.CardData.CardNumber = "41111111111111";
            authNet.PaymentData.CardData.ExpirationMonth = 1;
            authNet.PaymentData.CardData.ExpirationYear = 2009;
            authNet.PaymentData.CardData.SecurityCode = "1234";

            // Set customer billing data
            authNet.PaymentData.Customer.EmailAddress = "test@donotresolve.com";
            authNet.PaymentData.Customer.FirstName = "John";
            authNet.PaymentData.Customer.LastName = "Public";
            //authNet.PaymentData.Customer.Company = "Acme Corp.";
            authNet.PaymentData.Customer.AddressLine1 = "123 Fake Street";
            //authNet.PaymentData.Customer.AddressLine2 = "Suite 1234";
            authNet.PaymentData.Customer.City = "MyCity";
            authNet.PaymentData.Customer.State = "ZZ";
            authNet.PaymentData.Customer.PostalCode = "12345";
            authNet.PaymentData.Customer.Country = "US";                       // Use ISO Code
            authNet.PaymentData.Customer.CustomerId = "test@donotresolve.com"; // Choose something unique to this customer
            authNet.PaymentData.Customer.IPAddress = "127.0.0.1";              // for tracking
            //authNet.PaymentData.Customer.PhoneNumber = "1234567890";
            //authNet.PaymentData.Customer.FaxNumber = "1234567890";


            // Tranasction Settings
            authNet.PaymentData.Transaction.Amount = 1.23M;                                // The actual amount to charge, refund, auth, capture or void
            authNet.PaymentData.Transaction.MerchantDescription = "Order From MoneyBaby";  // a description for the transaction
            authNet.PaymentData.Transaction.MerchantInvoiceNumber = "ABC123";              // a unique transaction id from you
            //authNet.PaymentData.Transaction.PreviousTransactionReferenceNumber = "123";  // Required for VOID and REFUND transactions. 


            // Call your desired action
            TransactionResultData result = authNet.Charge();
            if (result.Suceeded)
            {
                Console.WriteLine("Success!");
            }
            else
            {
                Console.WriteLine("Failed!");
                Console.WriteLine(result.ResponseCodeDescription);
                foreach (BVSoftware.MoneyBaby.TransactionMessage m in result.Messages)
                {
                    switch (m.MessageType)
                    {
                        case BVSoftware.MoneyBaby.TransactionMessageType.Error:
                            Console.WriteLine("ERROR:");
                            break;
                        case BVSoftware.MoneyBaby.TransactionMessageType.Warning:
                            Console.WriteLine("WARNING:");
                            break;
                        case BVSoftware.MoneyBaby.TransactionMessageType.Information:
                            Console.WriteLine("INFO:");
                            break;
                        default:
                            Console.WriteLine("Other:");
                            break;
                    }
                    Console.WriteLine(m.Description);
                }
            }

            Console.WriteLine("**********************************");
            Console.WriteLine(result.AVSCode);                  // Address Verification Code
            Console.WriteLine(result.AVSCodeDescription);       // Friendly description of AVS
            Console.WriteLine(result.CVVCode);                  // Security Code Results Code
            Console.WriteLine(result.CVVCodeDescription);       // Friendly description of CVV Code
            Console.WriteLine(result.ResponseCode);             // Response Code from Gateway for this transaction only
            Console.WriteLine(result.ResponseCodeDescription);  // Friendly description of response code
            // You'll want to store the transaction reference number from all calls so 
            // it can be used later in refunds and voids
            Console.WriteLine(result.TransactionReferenceNumber);

            Console.WriteLine("");
            Console.WriteLine("Finished Authorize.Net Sample");
        }
    }
}
